package com.slf.engine.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.web.context.WebApplicationContext;

import com.slf.engine.bo.Order;
import com.slf.engine.bo.WaitBean;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBusService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.utils.KeyedDigestMD5;

public class NoticeRecevAction extends Action{

	private static final Logger log = Logger.getLogger(NoticeRecevAction.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
	{
		// TODO Auto-generated method stub
		PrintWriter out = null;
		DynaActionForm orderForm = (DynaActionForm)form;
		String OrderID = orderForm.getString("orderid");
		String OrderStatus = orderForm.getString("status");
		String ChgMobile = orderForm.getString("mobilenum");
		String Amount = orderForm.getString("price");
		log.log(Priority.INFO, "接收到通知结果OrderID:" + OrderID + " ChgMobile:" + ChgMobile + 
				" Amount:" + Amount + " OrderStatus:" + OrderStatus);
		IBusService busService = new BusServiceImpl((WebApplicationContext)this.servlet.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
		Order order = new Order();
		order.setAccount(ChgMobile);
		order.setOrderId(OrderID);
		order.setMoney(Amount);
		synchronized (SysContext.QUERY_MAP) {
			WaitBean waitBean = SysContext.QUERY_MAP.get(order.getOrderId());
			if(waitBean != null)
			{
				order = waitBean.getOrder();
				waitBean.removeFromList();
			}
		}
		if("2".equals(OrderStatus))
		{
			order.setCljg("充值成功");
			order.setClbz(1);
		}else if("3".equals(OrderStatus)){
			order.setCljg("充值失败");
			order.setClbz(2);
		}else if("4".equals(OrderStatus)){
			order.setClbz(SysContants.CZ_UNKNOW);
			order.setCljg("部分到帐");
		}else{
			return null;
		}
		busService.doBusResult(order);
		try {
			out = response.getWriter();
			out.write("2");
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

}
